package net.tycmc.cty.ctyms.timejob.iems.job;

import java.util.HashMap;

import net.tycmc.cty.ctyms.timejob.function.monitore.CheckSelfAndReport;
import net.tycmc.cty.ctyms.timejob.function.monitore.ControlHandler;
import net.tycmc.cty.ctyms.timejob.function.monitore.HeartBeat;
import net.tycmc.cty.ctyms.timejob.iems.service.UpdateEBPStateService;

import org.apache.commons.configuration.ConfigurationException;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.tycmc.functions.util.DynamicReadConfig;

/**
 * 定时作业：更新EBP状态（更新基础数据--载重量和EBP开关设定 模块中的EBP开关（EBP开关使能状态）值）
 * 每天零点四十五分执行一次
 * @author lixiaofan
 */
public class UpdateEBPStateJob extends QuartzJobBean {
	
	private UpdateEBPStateService jobData;
	private HashMap<String, Object> param;

	public UpdateEBPStateService getJobData() {
		return jobData;
	}
	
	public void setJobData(UpdateEBPStateService jobData) {
		this.jobData = jobData;
	}

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		if(ControlHandler.isControl()){
			this.reportHeartBeat();
			this.checkSelf();
		}
		this.jobData.UpdateEBPState();
	}
	
	/**
	 * 上报心跳信号
	 **/
	private void reportHeartBeat() {
		HeartBeat hbeat = new HeartBeat();
		this.param = new HashMap<String,Object>();
		String updateEBPState = "10";
		try {
			updateEBPState = DynamicReadConfig.getConfigProperty("config.properties", "updateEBPStateKey");
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		this.param.put("jobID", updateEBPState);
		this.param.put("jobName", "更新EBP状态");
		hbeat.produceAndReportHeateBeat(this.param);
	}
	/**
	 * 自检，如果自检未通过则进行报警
	 **/
	private void checkSelf() {
		CheckSelfAndReport csar = new CheckSelfAndReport();
		csar.checkIfFailReport(this.param);
	}
}
